using System;
using System.Collections.Generic;
using System.Text;
using IGlobalist.Cms.Model;


namespace IGlobalist.Cms.Bll
{
    public class OrderController : BaseController
    {
        public static long InsertOrder(IOrder order)
        {
            long id = _Ds.InsertObject((ICmsObject)order);

            InsertOrderLine(id, order.OrderLineList);

            return id;
        }

        private static void InsertOrderLine(long orderID, OrderLineList orderLineList)
        {
            if (orderLineList != null && orderLineList.Count > 0)
            {
                foreach (IOrderLine orderLine in orderLineList)
                {
                    orderLine.ParentID = orderID;
                    long orderLineID = OrderLineController.InsertOrderLine(orderLine);
                    // _Ds.InsertObjectRelation(orderID, orderLineID, (short)CmsObjectType.Order);
                }
               
            }
        }

        public static  IList<IOrder> GetOrderList()
        {
            return ConvertListObject2OrderList(_Ds.GetObjectListAllByClassTypeID((short)CmsObjectType.Order));
        }

        public static IList<IOrder> ConvertListObject2OrderList(IList<ICmsObject> list)
        {
            IList<IOrder> listOrders = new List<IOrder>();
            if (list != null)
                foreach (ICmsObject order in list)
                {
                    listOrders.Add(ConvertCmsObject2Order(order));
                }
            return listOrders;
        }

        public static IOrder ConvertCmsObject2Order(ICmsObject obj)
        {

            IOrder order = new Order();
            order.FromCmsObject(obj);
            if (obj == null) return null;
            order.OrderLineList = OrderLineController.GetOrderLineListLelateToObjectID(obj.ID);
            return order;
        }

        public static IOrder GetOrder(long id)
        {
            return ConvertCmsObject2Order(_Ds.GetObject(id));
        }

    }
}
